package com.xinxin.facelinker.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.easemob.chatuidemo.R;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.xinxin.facelinker.Config;
import com.xinxin.facelinker.adapter.ShowMyMotionAdapter;
import com.xinxin.facelinker.domain.ShowMyMotion;

import java.util.ArrayList;
import java.util.List;

public class ShowUserMotionActivity extends Activity {

    private ListView lvUserMotion;
    private String other_account_num;
    private List<ShowMyMotion> list = new ArrayList<ShowMyMotion>();
    private HttpUtils httpUtils = new HttpUtils();
    private View btnBack;
    private ShowMyMotionAdapter adapter;
    private ImageView ivMyMotionBackground;
    private ImageView ivMyMotionPhoto;
    private TextView tvNickname;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_user_motion);

        lvUserMotion = (ListView) findViewById(R.id.lvUserMotion);
        btnBack = findViewById(R.id.btnBack);
        other_account_num = getIntent().getStringExtra(Config.KEY_OTHER_ACCOUNT_NUM);
        lvUserMotion.setAdapter(adapter);

        getMyMotion();

        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void getMyMotion(){
        final RequestParams params = new RequestParams();
        params.addBodyParameter(Config.ACTION, Config.ACTION_SHOW_MY_MOTION);
        params.addBodyParameter(Config.KEY_MY_ACCOUNT_NUM, other_account_num);
        httpUtils.send(HttpRequest.HttpMethod.POST, Config.CATEGORIES_URL, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                Gson gson = new Gson();
                list = gson.fromJson(responseInfo.result, new TypeToken<ArrayList<ShowMyMotion>>() {
                }.getType());
                adapter = new ShowMyMotionAdapter(ShowUserMotionActivity.this, list);
                lvUserMotion.setAdapter(adapter);
                getHearder(list);

                lvUserMotion.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                        Intent intent = new Intent(ShowUserMotionActivity.this, ShowMotionActivity.class);
                        intent.putExtra(Config.KEY_MOTION_PHOTO_URL, list.get(position).getMotion_photo_url());
                        intent.putExtra(Config.KEY_MOTION_ID, list.get(position).getMotion_id());
                        intent.putExtra(Config.KEY_MOTION, list.get(position).getMotion());
                        intent.putExtra(Config.KEY_PHOTO_URL, list.get(position).getPhoto_url());
                        intent.putExtra(Config.KEY_DATE, list.get(position).getDate());
                        intent.putExtra(Config.KEY_NICKNAME, list.get(position).getNickname());
                        startActivity(intent);
                    }
                });
            }

            @Override
            public void onFailure(HttpException e, String s) {
                Toast.makeText(ShowUserMotionActivity.this, R.string.show_my_motion_fail, Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void getHearder(List<ShowMyMotion> list){

        ImageLoader imageLoader=ImageLoader.getInstance();
        LayoutInflater layoutInflater=getLayoutInflater();
        View header = layoutInflater.inflate(R.layout.layout_show_my_motion_header, null);

        ivMyMotionBackground = (ImageView) header.findViewById(R.id.ivMyMotionBackground);
        ivMyMotionPhoto = (ImageView) header.findViewById(R.id.ivMyMotionPhoto);
        tvNickname= (TextView) header.findViewById(R.id.tvNickname);

        ivMyMotionPhoto.setScaleType(ImageView.ScaleType.CENTER_CROP);
        ivMyMotionBackground.setScaleType(ImageView.ScaleType.CENTER_CROP);

        ivMyMotionBackground.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            }
        });
        ivMyMotionPhoto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            }
        });

        imageLoader.displayImage(list.get(0).getPhoto_url(), ivMyMotionBackground);
        imageLoader.displayImage(list.get(0).getPhoto_url(), ivMyMotionPhoto);
        tvNickname.setText(list.get(0).getNickname());

        lvUserMotion.addHeaderView(header);
    }
}
